package com.wolfcode.customer.service.impl;

import java.util.List;

import com.wolfcode.common.utils.DateUtils;
import com.wolfcode.common.utils.PhoneUtil;
import com.wolfcode.common.utils.SecurityUtils;
import com.wolfcode.common.utils.StringUtils;
import com.wolfcode.customer.domain.vo.CustomerLinkmaneVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.wolfcode.customer.mapper.CustomerLinkmaneMapper;
import com.wolfcode.customer.domain.CustomerLinkmane;
import com.wolfcode.customer.service.ICustomerLinkmaneService;
import org.springframework.util.Assert;

/**
 * 客户联系人Service业务层处理
 *
 * @author wolfcode
 * @date 2022-12-09
 */
@Service
public class CustomerLinkmaneServiceImpl implements ICustomerLinkmaneService
{
    @Autowired
    private CustomerLinkmaneMapper customerLinkmaneMapper;

    /**
     * 查询客户联系人
     *
     * @param id 客户联系人主键
     * @return 客户联系人
     */
    @Override
    public CustomerLinkmane selectCustomerLinkmaneById(Long id)
    {
        return customerLinkmaneMapper.selectCustomerLinkmaneById(id);
    }

    /**
     * 查询客户联系人列表
     *
     * @param customerLinkmane 客户联系人
     * @return 客户联系人
     */
    @Override
    public List<CustomerLinkmane> selectCustomerLinkmaneList(CustomerLinkmane customerLinkmane)
    {
        return customerLinkmaneMapper.selectCustomerLinkmaneList(customerLinkmane);
    }

    /**
     * 新增客户联系人
     *
     * @param customerLinkmane 客户联系人
     * @return 结果
     */
    @Override
    public int insertCustomerLinkmane(CustomerLinkmane customerLinkmane)
    {
        Assert.notNull(customerLinkmane,"非法参数");
        Assert.state(PhoneUtil.isMobileNumber(customerLinkmane.getPhone()),"联系方式格式不正确");
        Assert.state(customerLinkmane.getAge()<100&&customerLinkmane.getAge()>0,"年龄只能在0~100之间");
        customerLinkmane.setInputUser(SecurityUtils.getUsername());
        customerLinkmane.setInputTime(DateUtils.getNowDate());
        return customerLinkmaneMapper.insertCustomerLinkmane(customerLinkmane);
    }

    /**
     * 修改客户联系人
     *
     * @param customerLinkmaneVo 客户联系人
     * @return 结果
     */
    @Override
    public int updateCustomerLinkmane(CustomerLinkmaneVo customerLinkmaneVo)
    {
        // 校验参数
        Assert.notNull(customerLinkmaneVo,"联系人参数异常");
        Assert.notNull(customerLinkmaneVo.getCustomerId(),"未选择客户");
        Assert.state(!StringUtils.isEmpty(customerLinkmaneVo.getLinkman())
                && customerLinkmaneVo.getLinkman().length() <= 30,"联系人名未写或字数过大");
        Assert.state(PhoneUtil.isMobileNumber(customerLinkmaneVo.getPhone()),"电话格式不对");
        Assert.notNull(customerLinkmaneVo.getGender(),"未选择性别");
        Assert.state(customerLinkmaneVo.getAge() != null
                && customerLinkmaneVo.getAge() > 0
                && customerLinkmaneVo.getAge() <= 100, "输入的年龄不合法");
        Assert.state(!StringUtils.isEmpty(customerLinkmaneVo.getPosition())
                && customerLinkmaneVo.getPosition().length() <= 20,"未输入职位");
        Assert.state(!StringUtils.isEmpty(customerLinkmaneVo.getDepartment())
                &&customerLinkmaneVo.getDepartment().length() <= 20,"未输入部门");
        Assert.notNull(customerLinkmaneVo.getPositionState(),"任职状态未选");

        // 拷贝前端传递参数到联系人对象中
        CustomerLinkmane customerLinkmane = new CustomerLinkmane();
        BeanUtils.copyProperties(customerLinkmaneVo,customerLinkmane);
        return customerLinkmaneMapper.updateCustomerLinkmane(customerLinkmane);
    }

    /**
     * 批量删除客户联系人
     *
     * @param ids 需要删除的客户联系人主键
     * @return 结果
     */
    @Override
    public int deleteCustomerLinkmaneByIds(Long[] ids)
    {
        return customerLinkmaneMapper.deleteCustomerLinkmaneByIds(ids);
    }

    /**
     * 删除客户联系人信息
     *
     * @param id 客户联系人主键
     * @return 结果
     */
    @Override
    public int deleteCustomerLinkmaneById(Long id)
    {
        Assert.notNull(id,"参数异常");
        CustomerLinkmane customerLinkmane = customerLinkmaneMapper.selectCustomerLinkmaneById(id);
        Assert.state(!(customerLinkmane.getPositionState().equals("1")), "在职员工不能删除");
        return customerLinkmaneMapper.deleteCustomerLinkmaneById(id);
    }
}
